#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    resize(600, 800);

    setStatusBar(new QStatusBar());
    statusBar()->showMessage(tr("Ready"));

    //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    servList = new ServListWidget();
    QDockWidget *dockWidget = new QDockWidget(tr("Console (Chat and Log)"), this);
    Console* consol = new Console();   
    lobby = new LobbyWidget(true);
    game = new SFMLWindow(this, QPoint(20, 20), QSize(360, 360));
    communicator = new Communicator(game);
    gameWidget = new QWidget();
    exitGame = new QPushButton("Leave Game");
    stackW = new QStackedWidget();

    QVBoxLayout* layout = new QVBoxLayout;
    layout->addWidget(game);
    layout->addWidget(exitGame);
    gameWidget->setLayout(layout);

    dockWidget->setAllowedAreas(Qt::BottomDockWidgetArea);

    dockWidget->setWidget(consol);
    dockWidget->setFeatures(QDockWidget::DockWidgetMovable
                            | QDockWidget::DockWidgetFloatable);
    addDockWidget(Qt::BottomDockWidgetArea, dockWidget);

    stackW->addWidget(servList);
    stackW->addWidget(lobby);
    stackW->addWidget(gameWidget);
    stackW->setCurrentWidget(servList);


    connect(lobby, SIGNAL(chatMsgOut(QString)), communicator, SLOT(chatMsg(QString)));
    connect(communicator, SIGNAL(playerListChanged(std::vector<plInLobby>*)), lobby, SLOT(changeList(std::vector<plInLobby>*)));

    connect(servList->refreshList_b, SIGNAL(clicked(bool)), communicator, SLOT(refreshServerList()));
    connect(servList->hostGame_b, SIGNAL(clicked(bool)), communicator, SLOT(becomeServer(bool)));
    connect(servList->hostGame_b, SIGNAL(clicked(bool)), this, SLOT(setLobby()));
    connect(servList->lineEdit, SIGNAL(textChanged(QString)), communicator, SLOT(changeName(QString)));
    connect(servList->ipEdit, SIGNAL(textChanged(QString)), communicator, SLOT(changeStrIp(QString)));
    connect(servList, SIGNAL(connectTry(sf::IpAddress)), communicator, SLOT(tryToConnect(sf::IpAddress)));
    connect(servList->connetByIp_b, SIGNAL(clicked(bool)), communicator, SLOT(strConnection()));
    connect(communicator, SIGNAL(servListChanged(std::vector<Host>*)), servList, SLOT(redrawList(std::vector<Host>*)));

    connect(communicator, SIGNAL(statusOut(QString)), statusBar(), SLOT(showMessage(QString)));
    connect(communicator, SIGNAL(consoleOut(QString,Qt::GlobalColor)), consol, SLOT(enterMessage(QString,Qt::GlobalColor)));
    connect(communicator, SIGNAL(comeToLobby()), this, SLOT(setLobby()));
    connect(communicator, SIGNAL(leaveLobby()), this, SLOT(setMainmenu()));
    connect(lobby->b_exitBtn, SIGNAL(clicked()), this, SLOT(setMainmenu()));
    connect(lobby->b_exitBtn, SIGNAL(clicked()), communicator, SLOT(leaveGame()));
    connect(lobby->b_startGameBtn, SIGNAL(clicked()), this, SLOT(startGame()));
    connect(lobby->b_startGameBtn, SIGNAL(clicked()), communicator, SLOT(startGame()));
    connect(lobby->b_startGameBtn, SIGNAL(clicked()), game, SLOT(start()));
    connect(exitGame, SIGNAL(clicked()), this, SLOT(setMainmenu()));
    connect(exitGame, SIGNAL(clicked()), communicator, SLOT(leaveGame()));
    connect(game, SIGNAL(tankOut(tankInfo)), communicator, SLOT(tankHere(tankInfo)));
    connect(communicator, SIGNAL(startGameSignal()), this, SLOT(startGame()));
    connect(game, SIGNAL(shootOut(shoot)), communicator, SLOT(sendShoot(shoot)));

    setCentralWidget(stackW);
    //setMainmenu();

}

MainWindow::~MainWindow()
{

}

void MainWindow::setLobby() {

    servList->redrawList(NULL);
    stackW->setCurrentWidget(lobby);
}

void MainWindow::setMainmenu() {

    stackW->setCurrentWidget(servList);

}

void MainWindow::startGame() {

    stackW->setCurrentWidget(gameWidget);

}


